import { Component, OnInit } from "@angular/core";
export interface LogInfoData {
  time: string | number;
  user: string;
  content: string;
  level: string;
}
@Component({
  selector: "app-log-info",
  templateUrl: "./log-info.component.html",
  styleUrls: ["./log-info.component.scss"]
})
export class LogInfoComponent implements OnInit {
  sortName: string | null = null;
  sortValue: string | null = null;
  dataList: Array<LogInfoData> = [
    {
      time: "2018-11-01",
      user: "张三",
      content: "登录",
      level: "1"
    },
    {
      time: "2018-11-02",
      user: "李四",
      content: "查看基础信息",
      level: "1"
    },
    {
      time: "2018-11-03",
      user: "	管理员",
      content: "新增用户",
      level: "2"
    },
    {
      time: "2018-11-04",
      user: "田昊",
      content: "修改隧洞信息",
      level: "3"
    },
    {
      time: "2018-11-05",
      user: "刘攀",
      content: "启动预报",
      level: "4"
    }
  ];
  tableData: Array<LogInfoData> = [...this.dataList];

  constructor() {}

  ngOnInit() {}

  sort(sort: { key: string; value: string }): void {
    this.sortName = sort.key;
    this.sortValue = sort.value;
    this.search();
  }

  search(): void {
    // sort data
    if (this.sortName && this.sortValue) {
      this.tableData = this.dataList.sort((a, b) =>
        this.sortValue === "ascend"
          ? a[this.sortName!] > b[this.sortName!]
            ? 1
            : -1
          : b[this.sortName!] > a[this.sortName!]
          ? 1
          : -1
      );
    } else {
      this.tableData = [...this.dataList];
    }
  }
}
